app.controller('participantscontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http)
{
	$scope.$on('eventReceived', function(event, args) {
		console.info('Event received');
		$scope.event = args.event;
		$scope.getParticipantsCommand();
	});

	/**
	* Get participants
	*/
	$scope.getParticipantsCommand = function()
	{
		console.info("getParticipantsCommand - called"); 
		$scope.participants = [];
		$scope.loadingParticipants = true;

		$http.post($scope.servicebase, { "service" : "getParticipants", "id" : $scope.event.Id}).
		success(function(data, status) 
		{
			console.info("getParticipantsCommand - call success"); 
			$scope.data = data;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.participants = $scope.data.response;
			
			$scope.participants.forEach(function(eventSubscription) {
				eventSubscription.IsPartner = eventSubscription.IsPartner == 1 ? true : false;
			});
			
			$scope.loadingParticipants = false;
		})
		.
		error(function(data, status) 
		{
			console.error("getParticipantsCommand - call failed"); 	
			$scope.loadingParticipants = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getParticipantsCommand - data : " + $scope.data); 				
		});
	}
	
	/**
	* Set if participant is partner to this event
	*/
	$scope.participantIsPartnerCommand = function(eventSubscriptionId)
	{
		console.info("participantIsPartnerCommand - called"); 
		
		$http.post($scope.servicebase, { "service" : "userIsPartner", "eventSubscriptionId" : eventSubscriptionId.Id, "isPartner" : eventSubscriptionId.IsPartner}).
		success(function(data, status) 
		{
			console.info(JSON.stringify(data));
			console.info("participantIsPartnerCommand - call success"); 
			$scope.data = data;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
		})
		.
		error(function(data, status) 
		{
			console.error("participantIsPartnerCommand - call failed"); 	
			$scope.loadingParticipants = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("participantIsPartnerCommand - data : " + $scope.data); 				
		});
	}
	
	/**
	*	Load the events component
	*/
	$scope.loadParticipantsComponentCommand = function() 
	{
		console.info("loadParticipantsComponentCommand - called");
		$scope.participants = [];
		//$scope.getParticipantsCommand();
	};
	
	$scope.loadParticipantsComponentCommand();
}]);